package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.beautyescort.CrossBeautyEscortMgr;
import com.yanqu.road.server.manager.beautyescort.CrossEscortPlayerWrap;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.C_BEAUTY_ESCORT_ROB_CART, desc = "劫镖")
public class CrossBeautyEscortRobCartCmd extends CrossCommand {

    long userId;
    long userServerId;
    long dartId;
    List<Integer> buyBeautyList;
    List<BeautyEscortProto.BeautyEscortBeautyDispatchDataTemp> beautyDispatchDataTemps;
    int position;
    String consume;
    long playerId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortRobCartReqMsg reqMsg = BeautyEscortProto.CrossBeautyEscortRobCartReqMsg.parseFrom(packet.getBytes());
        buyBeautyList = reqMsg.getBuyBeautyList();
        dartId = reqMsg.getDartId();
        beautyDispatchDataTemps = reqMsg.getBeautyList();
        userId = reqMsg.getUserId();
        userServerId = reqMsg.getServerId();
        if(reqMsg.hasPosition()) {
            position = reqMsg.getPosition();
        }
        consume = reqMsg.getBuyConsume();
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossEscortPlayerWrap escortPlayer = CrossBeautyEscortMgr.getEscortPlayer(serverId, packet.getPlayerId());
        if(escortPlayer != null){
            if(userId < 100000 || userServerId == 0){
                //NPC
                ret = escortPlayer.robNpcCart((int)userId, dartId, buyBeautyList, CrossBeautyEscortMgr.parseBeautyDispatch(beautyDispatchDataTemps), position, consume);
            }else {
                CrossEscortPlayerWrap targetPlayer = CrossBeautyEscortMgr.getEscortPlayer(userServerId, userId);
                if(targetPlayer != null){
                    ret = escortPlayer.robCart(targetPlayer, dartId, buyBeautyList, CrossBeautyEscortMgr.parseBeautyDispatch(beautyDispatchDataTemps), position, consume);
                }else {
                    ret = GameErrorCode.E_BEAUTY_ESCORT_USER_NOT_FOUND;
                }
            }
        }
    }

    @Override
    public void sendData() {
            if(ret != 0){
                BeautyEscortProto.CrossBeautyEscortRobCartRespMsg.Builder respMsg = BeautyEscortProto.CrossBeautyEscortRobCartRespMsg.newBuilder();
                respMsg.setRet(ret);
                respMsg.setWin(false);
                respMsg.addAllBeauty(beautyDispatchDataTemps);
                respMsg.setBuyConsume(consume);
                //respMsg.setEnemyCart(enemyCartBuilder);
                respMsg.addAllBuyBeauty(buyBeautyList);
                respMsg.setPosition(position);
                MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_BEAUTY_ESCORT_ROB_CART_FROM_CROSS, respMsg));
            }
    }
}
